package com.lingxu.module.collection.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.module.collection.entity.ModelAnalysis;
import com.lingxu.module.collection.entity.ModelAnalysisSearchVo;
import com.lingxu.module.collection.entity.ModelInfoNew;
import com.lingxu.module.collection.mapper.CountAnalysisMapper;
import com.lingxu.module.collection.service.ICountAnalysisService;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CountAnalysisServiceImpl implements ICountAnalysisService {

    @Resource
    private CountAnalysisMapper countAnalysisMapper;

    @Override
    public IPage<ModelAnalysis> getModelAnalysisPageList(Page<ModelAnalysis> page, ModelAnalysisSearchVo vo) {
        List<ModelInfoNew> list = countAnalysisMapper.getModelInfo();
        handelModelInfo(list);
        return countAnalysisMapper.getModelAnalysisPageList(page,vo);
    }

    @Override
    public void handelModelAnalysis() {
        List<ModelInfoNew> list = countAnalysisMapper.getModelInfo();
        handelModelInfo(list);
    }

    private void handelModelInfo(List<ModelInfoNew> modelInfoNewList) {
        for(ModelInfoNew modelInfo:modelInfoNewList){
            String content= modelInfo.getContent();
            Integer tablenum=countAnalysisMapper.getTableCount(modelInfo.getId());
            Integer relationnum=0;
            if(StringUtils.isNotBlank(content)){
                Map<String, Object> json = JSONObject.parseObject(modelInfo.getContent(),Map.class);
                if(json!=null){
                    List<Map<String, Object>> list = (List<Map<String, Object>>) json.get("nodes");
                    if(list!=null && list.size() > 0) {
                        for (Map<String, Object> map : list) {
                            Map<String, Object> data = (Map<String, Object>) map.get("data");
                            String nodeType = String.valueOf(data.get("nodeType")).replace("null","");
                            if(StringUtils.isNotBlank(nodeType) && "A".equals(nodeType.substring(0,1))){
                                if(!"A41".equals(nodeType) && !"A42".equals(nodeType)){
                                    relationnum++;
                                }
                            }
                        }
                    }
                }
            }
            if(tablenum<=2 && relationnum<=3){
                modelInfo.setComplexity("简单");
            }else if(tablenum>6 && relationnum>8){
                modelInfo.setComplexity("复杂");
            }else {
                modelInfo.setComplexity("一般");
            }
            modelInfo.setTablenum(tablenum.toString());
            modelInfo.setRelationnum(relationnum.toString());
            countAnalysisMapper.updateModeInfoById(modelInfo);
        }
    }
}
